<template>
  <div class="xiecheng">
    <div class="admin_main_block">
      <div class="admin_main_block_top">
        <div class="admin_main_block_left">
          <div>{{ i18n('organization.harbor.form') }}</div>
        </div>

        <div class="admin_main_block_right">
          <div>
            <el-button icon="Back" @click="router.go(-1)">
              {{ i18n('common.return') }}
            </el-button>
          </div>
        </div>
      </div>

      <div class="admin_form_main">
        <el-form label-width="120px" ref="formRef" :model="dataForm" :rules="dataRule">
          <el-card class="box-card mt10" shadow="never">
            <template v-slot:header>
              <div class="clearfix">
                <span>{{ i18n('organization.apply.resource.info') }}</span>
              </div>
            </template>
            <div class="text item text-center">
              <el-descriptions border>
                <el-descriptions-item width="200" :label="i18n('organization.apply.inquiry_harbor')" label-align="right" :span="3">
                  {{ dataForm.inquiry_harbor }}
                </el-descriptions-item>
                <el-descriptions-item width="200" :label="i18n('organization.apply.resource.url')" label-align="right" :span="3">
                  <el-row :gutter="20">
                    <el-col :span="2" v-for="(v, k) in dataForm.resource" :key="k">
                      <view class="grid">
                        <el-link :href="v.url" :underline="false" target="_blank">
                          <img v-if="v.url" src="@/assets/images/default/file.png" class="avatar-upload"/>
                        </el-link>
                        <el-text class="filename" line-clamp="1">
                          {{ v.filename }}
                        </el-text>
                      </view>
                    </el-col>
                  </el-row>
                </el-descriptions-item>
              </el-descriptions>
            </div>
          </el-card>

          <el-card class="box-card mt10" shadow="never">
            <template v-slot:header>
              <div class="clearfix">
                <span>{{ i18n('organization.harbor.harbor_setting') }}</span>
              </div>
            </template>
            <div class="text item">
              <el-form-item :label="i18n('organization.harbor.harbor_code')" prop="harbor_code">
                <el-select v-model="dataForm.harbor_code" :placeholder="i18n('common.please_select') + i18n('organization.harbor.harbor_code')" :remote-method="loadHarborList" :loading="loading" reserve-keyword filterable remote multiple clearable>
                  <el-option v-for="(v, k) in harborList" :label="v.chinese_name" :key="k" :value="v.code">
                  </el-option>
                </el-select>
              </el-form-item>
            </div>
          </el-card>


          <el-card class="box-card mt10" shadow="never">
            <div class="text item">
              <el-form-item>
                <el-button
                  v-if="isAuth('module:organization:harbor:handle')"
                  type="primary"
                  @click="dataFormSubmit(formRef)">
                  {{ i18n('common.confirm') }}
                </el-button>
                <el-button @click="resetForm(formRef)">
                  {{ i18n('common.reset') }}
                </el-button>
              </el-form-item>
            </div>
          </el-card>
        </el-form>
      </div>
    </div>
  </div>
</template>

<script setup name="organization-harbor-form">
  import { useBase } from "@/hooks/base/useBase"
  import { useForm } from "@/hooks/form/useForm"

  const { router, query, i18n, isAuth } = useBase()

  const model = ref('organization/harbor')

  const harborList = ref([])
  const loading = ref(false)

  const dataForm = reactive({
    organization_id: 0,
    inquiry_harbor: '',
    harbor_code: [],
    resource: [],
  })

  const dataRule = reactive({})

  const {
    formRef,
    dataFormSubmit,
    resetForm
  } = useForm(model.value, dataForm)

  const init = (form) => {
    dataForm.organization_id = query.organization_id || 0

    nextTick(() => {
      form.value.resetFields()
      if (dataForm.organization_id) {
        $http({
          url: $http.adornUrl(`/${model.value}/data`),
          method: 'get',
          params: $http.adornParams({
            organization_id: dataForm.organization_id
          }),
        }).then(({ data }) => {
          if (data && data.status === 200) {
            dataForm.id = data.data.id
          }
        })
      }
    })
  }

  const loadApplyData = () => {
    $http({
      url: $http.adornUrl(`/organization/apply/data`),
      method: 'get',
      params: $http.adornParams({
        'organization_id': dataForm.organization_id
      }),
    }).then(({ data }) => {
      if (data && data.status === 200) {
        dataForm.inquiry_harbor = data.data.inquiry_harbor
        dataForm.resource = data.data.resource
      }
    })
  }

  const loadHarborList = (keyword) => {
    if (keyword && keyword.length > 1) {
      loading.value = true
      $http({
        url: $http.adornUrl('/business/harbor/select'),
        method: 'get',
        params: $http.adornParams({
          keyword: keyword
        }),
      }).then(({ data }) => {
        if (data && data.status === 200) {
          harborList.value = data.data
          loading.value = false
        } else {
          $message.error(data.message)
        }
      })
    } else {
      harborList.value = []
      loading.value = false
    }
  }

  onMounted(() => {
    init(formRef)

    loadApplyData()
  })
</script>

<style lang="scss" scoped>
  .option-left {
    float: left;
  }
  .option-right {
    float: right;
    color: var(--el-text-color-secondary);
    font-size: 13px;
  }
</style>
